package br.com.dallasmotopecas.util.senha;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import br.com.dallasmotopecas.util.validacao.ValidarObjeto;

import sun.misc.BASE64Encoder;

public final class Criptografar
{
	public static String criptografia(String entrada)
	{
		MessageDigest digest = null;
		BASE64Encoder encoder = null;
		String senha = "";
		try
		{
			if (ValidarObjeto.isStringValida(entrada))
			{
				digest = MessageDigest.getInstance("MD5");
				digest.update(entrada.getBytes());
				encoder = new BASE64Encoder();
				senha = encoder.encode(digest.digest());
				return senha;
			}
		}
		catch(NoSuchAlgorithmException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (digest != null)
			{
				digest.reset();
				digest = null;
			}
			
			if (encoder != null)
				encoder = null;
		}
		return senha;
	}
}
